﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.Web.Mvc;

using Common;
using AutoFacUtils;
using CashFlow.Web.Service;
using CashFlow.Web.MyActionResult;
using System.Text;

namespace CashFlow.Web.Common
{
    public class MyCommonTool
    {
        private static IUserService GetUserService() { return AutoFacHelper.Get<IUserService>(); }

        public static bool IsRootUser()
        {
            var service = GetUserService();
            return service.IsRootUser();
        }

        public static bool IsShopManager()
        {
            var service = GetUserService();
            return service.IsShopManager();
        }

        public static string GetOrderDate()
        {
            DateTime date = DateTime.Now;

            if (date.Hour < 15)
            {
                date = date.AddDays(-1);
            }

            return date.ToString("yyyy-MM-dd");
        }

        public static ActionResult CreateLayUITableResult(
            bool success
            , string msg
            , object data
            , int totalCount = 0)
        {
            var obj = new
            {
                Success = success,
                Msg = msg,
                Data = data,
                Total = totalCount
            };
            return new MyJsonResult(obj);
        }

        public static ActionResult CreateOperationResult(
                    bool success
                    , string msg
                    , object data)
        {
            var obj = new
            {
                Success = success,
                Msg = msg,
                Data = data
            };
            return new MyJsonResult(obj, JsonRequestBehavior.DenyGet);
        }

        public static bool IsReleaseOrderTime()
        {
            var hour = DateTime.Now.Hour;
            if (hour >= MyConfig.ReleaseOrderHourStart
                && hour <= MyConfig.ReleaseOrderHourEnd)
            {
                return true;
            }
            return false;
        }

        public static string CreateOrderNo()
        {
            return string.Format("E{0}", DateTime.Now.ToString("yyyyMMddHHmmssfffff"));
        }

        public static string GetProductName()
        {
            int index = RandTool.CreateRandValWithMinMax(0,
                MyConfig.ProductNames.Count - 1);
            return MyConfig.ProductNames.ElementAt(index);
        }
    }
}